package com.gidgabs.modules.api.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gidgabs.common.exception.RRException;
import com.gidgabs.common.utils.*;
import com.gidgabs.common.validator.Assert;
import com.gidgabs.dto.TbUserDto;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.dao.TbUserDao;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.YunXinUtil;
import io.swagger.models.auth.In;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wuye on 2017/11/24.
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserDao, TbUser> implements TbUserService {

    @Autowired
    private TbUserDao userDao;
    @Autowired private HuanXinUtil huanXinUtil;

    @Override
    public TbUser queryByMobile(String mobile) {
        return userDao.queryByMobile(mobile);
    }

    @Override
    public List<TbUser> queryList(Map<String, Object> map) {
        return userDao.queryList(map);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void update(TbUser tbuser) {
        userDao.updateByPrimaryKey(tbuser);
    }

    @Override
    public void updatePsByUserId(TbUser tbyser) {
        userDao.updateByPrimaryKey(tbyser);
    }

    @Override
    public Page<TbUser> queryPageList(Page<TbUser> page, Map<String, Object> map) {
        page.setRecords(userDao.queryPageList(page, map));
        return page;
    }
    @Override
    public TbUser queryById(Integer id) {
        return userDao.selectByPrimaryKey(Long.valueOf(id));
    }

    @Override
    public TbUser queryObject(TbUser user) {
        return userDao.queryObject(user);
    }

    @Override
    public List<TbUser> queryListObject(TbUser user) {
        return userDao.queryListObject(user);
    }

    @Override
    public TbUser foundById(Long userId) {
        return userDao.selectByPrimaryKey(userId);
    }

    @Override
    public void save(TbUser tbUser) {
        userDao.save(tbUser);
        //注册环信用户
        //huanXinUtil.registHuaxin(String.valueOf(tbUser.getUserId()),tbUser.getEmail());
        String yunxinToken = YunXinUtil.registYunxin(String.valueOf(tbUser.getUserId()), tbUser.getEmail());
        tbUser.setYunXinToken(yunxinToken);
        userDao.updateNotNullTbUserById(tbUser);
    }

    @Override
    public void updateByUserCode(TbUser tbUser) {
        userDao.updateByUserCode(tbUser);
    }

    @Override
    public Integer login(String mobile, String password) {
        TbUser user = queryByMobile(mobile);
        Assert.isNull(user, "手机号未注册");
        //密码错误
        if (!user.getPassword().equals(DigestUtils.sha256Hex(password))) {
            throw new RRException("手机号或密码错误");
        }

        return user.getUserId();
    }
    @Override
    public TbUser queryOpenId(String openid) {
        return userDao.queryOpenId(openid);
    }

    @Override
    public TbUser queryByEmail(String email) {
        TbUser tbUser=new TbUser();
        tbUser.setEmail(email);
        return  userDao.selectTbUserObjSingle(tbUser);
    }

    @Override
    public TbUser loginEmail(String email, String password) {
        TbUser user = queryByEmail(email);
        Assert.isNull(user, "邮箱未注册");
        //密码错误
        if (!user.getPassword().equals(DigestUtils.sha256Hex(password))) {
            throw new RRException("密码错误");
        }
        return user;
    }

    @Override
    public TbUser selectTbUserById(Integer id) {
        return userDao.selectTbUserById(id);
    }

    @Override
    public List<TbUserDto> selectTbUserByUserName(TbUser obj) {
        Assist assist = new Assist();
        assist.andEq("delete_type", DeleteType.NOT_DELETE.getKey())
                .andEq("choose_type",obj.getChooseType())
                .andLike("user_name","%"+obj.getUserName()+"%");
        List<TbUser>  tbUsers = userDao.selectTbUser(assist);
        List<TbUserDto>  tbUserDtos = new ArrayList<>();
        for (TbUser tbUser1: tbUsers) {
            tbUserDtos.add(tbUser1.toDto());
        }
        return tbUserDtos;
    }

    @Override
    public ReturnJson<TbUserDto> queryList(Integer pageSize, Integer pageNo, Integer chooseType) {
        Assist assist = new Assist();
        Assist.WhereOrder fans_count = Assist.order("fans_count", false);
        assist.andEq("delete_type", DeleteType.NOT_DELETE.getKey())
                .andEq("chooseType", chooseType);
        assist.setOrder(fans_count);
        assist.setRowSize(pageSize);
        assist.setStartRow(LangUtil.firstLimit(pageSize, pageNo));
        List<TbUser> tbUsers = userDao.selectTbUser(assist);
        long count = userDao.getTbUserRowCount(assist);
        List<TbUserDto>  tbUserDtos = new ArrayList<>();
        for (TbUser tbUser1: tbUsers) {
            tbUserDtos.add(tbUser1.toDto());
        }
        return ReturnJson.success("查询成功", tbUserDtos, count, pageSize, pageNo);
    }

}
